package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Consumer;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.service.ConsumerService;
import com.hmusic.mymusic.utils.Consts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Map;

/**
 * 用户Controller层
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;
    @Value("${avatorImages.dir}")
    private String realPath;

    /**
     * 管理员添加用户
     * @param consumer
     * @return
     */
    @PostMapping("/add")
    public Object addUser(@RequestBody Consumer consumer){
        JSONObject jsonObject = new JSONObject();
        try{
            consumerService.insert(consumer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加用户成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 用户注册
     * @param consumer
     *
     * @return
     */
    @PostMapping("/register")
    public Object register(@RequestBody Consumer consumer){
        JSONObject jsonObject = new JSONObject();
        try{
            consumerService.register(consumer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"注册成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 用户登录方法
     * @param consumer
     * @return
     */
    @PostMapping("/login/status")
    public Object userLogin(@RequestBody Consumer consumer){
        JSONObject jsonObject = new JSONObject();
        try{
            Consumer consumer1 = consumerService.oneConsumerByEmail(consumer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"登录成功");
            jsonObject.put("userid",consumer1.getId());
            jsonObject.put("username",consumer1.getUsername());
            jsonObject.put("useravator",consumer1.getAvator());
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 获取所有用户
     * @return
     */
    @GetMapping("/allUser")
    @ResponseBody
    public Object allUser(){
        return consumerService.allConsumer();
    }

    /**
     * 删除用户方法
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public Object deleteUser(String id){
        JSONObject jsonObject = new JSONObject();
         try{
             consumerService.delete(id);
             jsonObject.put(Consts.CODE,1);
             jsonObject.put(Consts.MSG,"删除用户成功");
         } catch (Exception e) {
             e.printStackTrace();
             jsonObject.put(Consts.CODE,0);
             jsonObject.put(Consts.MSG,"删除用户失败");
         }
         return jsonObject;
    }

    /**
     * 根据主键获取用户对象
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Object userDetail(String id){
        return consumerService.selectByPrimaryKey(id);
    }

    /**
     * 根据用户名模糊查询多个用户
     * @param username
     * @return
     */
    @GetMapping("/searchByName")
    @ResponseBody
    public Object searchByName(String username){
        return consumerService.selectByUserName(username);
    }

    /**
     * 修改用户方法
     * @param consumer
     * @return
     */
    @PostMapping("/update")
    public Object userUpdate(@RequestBody Consumer consumer){
        JSONObject jsonObject = new JSONObject();
        try{
            consumerService.update(consumer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"用户更新成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 上传/更新歌手图片
     */
    @PostMapping("/updateConsumerAvator")
    public Object updateConsumerAvator(@RequestParam("file") MultipartFile avatorFile, String id){
        JSONObject jsonObject = new JSONObject();
        if(avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传用户头像失败");
            return jsonObject;
        }
        //文件名=当前时间毫秒+原来文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        String filePath = realPath;
        System.out.println(filePath);
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //储存到数据库中的文件地址
        String storeAvatorPath = "/img/avatorImages/"+fileName;
        try{
            Consumer consumer = consumerService.selectByPrimaryKey(id);
            String oldConsumerAvatorUrl = consumer.getAvator();
            String newConsumerAvatorUrl = oldConsumerAvatorUrl.substring(18);
            System.out.println(newConsumerAvatorUrl);
            if (!newConsumerAvatorUrl.equals("userAvator0.jpeg")&&
                    !newConsumerAvatorUrl.equals("userAvator1.jpeg")&&
                    !newConsumerAvatorUrl.equals("userAvator2.jpeg")){
                File file=new File(filePath,newConsumerAvatorUrl);
                if(file.exists())file.delete();//删除原有头像地址
            }
            avatorFile.transferTo(dest);
            Consumer consumer1 = new Consumer();
            consumer1.setId(id);
            consumer1.setAvator(storeAvatorPath);
            consumerService.update(consumer1);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"上传用户头像成功");
            jsonObject.put("pic",storeAvatorPath);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传用户头像失败");
            return jsonObject;
        }
    }
    @GetMapping("/consumerCount")
    @ResponseBody
    public Object consumerCount(){
        return consumerService.consumerCount();
    }

    @GetMapping("/consumerGirlCount")
    @ResponseBody
    public Object consumerGirlCount(){
        return consumerService.consumerSexCount("0");
    }

    @GetMapping("/consumerBoyCount")
    @ResponseBody
    public Object consumerBoyCount(){
        return consumerService.consumerSexCount("1");
    }

    @GetMapping("/consumerDogCount")
    @ResponseBody
    public Object consumerDogCount(){
        return consumerService.consumerSexCount("2");
    }

}
